//
//  ByPlayLIstVC.m
//  caipiao_ios
//
//  Created by mc on 2018/7/30.
//  Copyright © 2018年 ZhangZhanbin. All rights reserved.
//

#import "ByPlayLIstVC.h"
#import "ByNoticeDetailVC.h"

@interface ByPlayLIstVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation ByPlayLIstVC

- (NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray arrayWithCapacity:1];
    }
    return _dataArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addLeftBackBarButtonItem];
    
    if (self.isPlayList) {
        self.title = @"玩法大集";
    }else{
        self.title = @"活动中心";
    }
    
    
    [self initViews];
    
    [self loadData];
    ByCoreRefreshGifHeader *header = [ByCoreRefreshGifHeader headerWithRefreshingBlock:^{
        [self loadData];
    }];
    self.tableView.mj_header = header;
    // Do any additional setup after loading the view.
}

- (void)loadData{
    
    
    [ByCoreToolAlert showLoadind:@"加载中"];
    if (self.isPlayList) {
        
        
        [ByFoundApi wanfajiheSuccess:^(ByNetBaseResult *result) {
            [ByCoreToolAlert hideHud];
            NSLog(@"玩法大集 = %@", result.data);
            [self.dataArray removeAllObjects];
            for (NSDictionary *dic in result.data[@"data"]) {
                ByNetModel *netModel = [[ByNetModel alloc] initContentWithDic:dic];
                [self.dataArray addObject:netModel];
            }
          
            [self.tableView.mj_header endRefreshing];
            [self.tableView reloadData];
            
        } failure:^(ByNetBaseResult *result) {
            [ByCoreToolAlert showFailureWithText:result.msg];
            [self.tableView.mj_header endRefreshing];
        }];
        
    }else{
        
        [ByFoundApi huodongSuccess:^(ByNetBaseResult *result) {
            [ByCoreToolAlert hideHud];
            [self.dataArray removeAllObjects];
            for (NSDictionary *dic in result.data[@"data"]) {
                ByNetModel *netModel = [[ByNetModel alloc] initContentWithDic:dic];
                [self.dataArray addObject:netModel];
            }
            [self.tableView.mj_header endRefreshing];
            [self.tableView reloadData];
            NSLog(@"活动中心 = %@", result.data);
        } failure:^(ByNetBaseResult *result) {
            [ByCoreToolAlert showFailureWithText:result.msg];
            [self.tableView.mj_header endRefreshing];
        }];
        
        
    }
  
}


- (void)initViews{
    
    self.view.backgroundColor = BY_BACKGROUND_COLOR;
    self.tableView = [[UITableView alloc] init];
    self.tableView.backgroundColor = BY_BACKGROUND_COLOR;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        if (@available(ios 11.0,*)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(0);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(0);
        } else {
            make.top.equalTo(self.view).offset(0);
            make.bottom.equalTo(self.view).offset(0);
        }
    }];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        cell.contentView.backgroundColor = BY_BACKGROUND_COLOR;
        
    }
    for (UIView *view in cell.contentView.subviews) {
        [view removeFromSuperview];
    }
    
    ByNetModel *netModel = [self.dataArray objectAtIndex:indexPath.row];
    
    UIView *backgroundView = [[UIView alloc] init];
    [cell.contentView addSubview:backgroundView];
    [backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(cell.contentView).offset(10);
        make.right.equalTo(cell.contentView).offset(-10);
        make.bottom.equalTo(cell.contentView).offset(-10);
        make.top.equalTo(cell.contentView).offset(0);
    }];
    backgroundView.layer.cornerRadius = 10;
    backgroundView.backgroundColor = [UIColor whiteColor];
    
    UIImageView *iconView = [[UIImageView alloc] init];
    [backgroundView addSubview:iconView];
    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backgroundView).offset(10);
        make.right.equalTo(backgroundView).offset(-10);
        make.top.equalTo(backgroundView).offset(10);
        make.bottom.equalTo(backgroundView).offset(-30);
    }];
    [iconView sd_setImageWithURL:[NSURL URLWithString:netModel.img] placeholderImage:[ByDefaultImageView createImageFromView:self.rectangularImage]];
    
    
    
    UILabel *titleLabel = [[UILabel alloc] init];
    [backgroundView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(iconView.mas_left).offset(0);
        make.top.equalTo(iconView.mas_bottom).offset(5);
        make.height.mas_equalTo(@20);
        make.right.equalTo(backgroundView).offset(-100);
    }];
    
    titleLabel.font = FONT(14);
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.textAlignment = NSTextAlignmentLeft;
    if (self.isPlayList) {
         titleLabel.text = netModel.name;
    }else{
        titleLabel.text = netModel.title;
    }
   
    
    
    UILabel *notesLabel = [[UILabel alloc] init];
    [backgroundView addSubview:notesLabel];
    [notesLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(iconView.mas_right).offset(0);
        make.centerY.equalTo(titleLabel.mas_centerY).offset(0);
        make.size.mas_offset(CGSizeMake(60, 20));
    }];
    notesLabel.font = FONT(12);
    notesLabel.textColor = [UIColor lightGrayColor];
    notesLabel.textAlignment = NSTextAlignmentRight;
    notesLabel.text = @"点击查看";
    
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return (SCREEN_WIDTH - 40) * 0.5 + 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     ByNetModel *netModel = [self.dataArray objectAtIndex:indexPath.row];
    if (self.isPlayList) {
        
        ByWebViewController *vc = [[ByWebViewController alloc] initWithTotalUrlString:[NSString stringWithFormat:@"%@/Wanfajiesao/jiesao?type=%@",BOYE_WEB_BASE_URL,netModel.ID]];
        vc.titleString = netModel.name;
        [self by_normalPush:vc hasNavi:YES];
        
    }else{
        
//        ByNoticeDetailVC *vc = [[ByNoticeDetailVC alloc] init];
//        vc.isNotice = NO;
//        vc.netModel = netModel;
//        [self by_normalPush:vc hasNavi:YES];
        
        ByWebViewController *vc = [[ByWebViewController alloc] initWithTotalUrlString:[NSString stringWithFormat:@"%@/Wanfajiesao/huodonginfo?article_id=%@",BOYE_WEB_BASE_URL,netModel.article_id]];
        vc.titleString = netModel.title;
        [self by_normalPush:vc hasNavi:YES];
        
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
